
<template>
  <div class="musicListRoot">
    <QualityOrder :quality='highquality' />
    <div class="navBar">

      <div class="left">
        <SortBox :sortList='sortListTag' @getSortList='getSortListTag' :currentTag='curTag' @itemBoxClick='itemBoxClick' />
      </div>

      <div class="right">
        <SecondNavBar @itemNavClick='itemNavClick' :currentTag='curTag' :navBarData='hotTags' />
      </div>
    </div>

    <ListCard :listCardData='musicList' @itemCardClick='itemCardClick' />
    <el-pagination @current-change="handleCurrentChange" :current-page="curPage" :page-size="50" background layout="prev, pager, next" :total="1000">
    </el-pagination>
  </div>
</template>

<script>
import { reqMusicList, reqSortListTag, reqHotTag, reqTheFirstOfHighquality } from '@/network/api'
import QualityOrder from './QualityOrder.vue'
import SecondNavBar from '@/components/secondNavBar/SecondNavBar.vue'
import ListCard from '@/components/listCard/ListCard.vue'
import SortBox from '@/components/sortBox/SortBox.vue'
import { reactive, toRefs, onMounted } from 'vue'
import { useRouter } from 'vue-router'
export default {
  name: 'musicListOrder',
  components: { QualityOrder, SecondNavBar, ListCard, SortBox },
  setup() {
    const data = reactive({
      highquality: {},
      curPage: 1,
      musicList: [],
      curTag: '',
      hotTags: [],
      sortListTag: []
    })
    const router = useRouter()
    const getMusicList = async () => {
      const res = await reqMusicList({
        cat: data.curTag.name,
        offset: (data.curPage - 1) * 50
      })
      data.musicList = res.playlists
    }

    const getHighquality = async () => {
      const res = await reqTheFirstOfHighquality()
      data.highquality = res.playlists[0]
    }
    const handleCurrentChange = p => {
      data.curPage = p
      getMusicList()
    }
    const getHotTag = async () => {
      const res = await reqHotTag()
      data.curTag = res.tags[0]
      data.hotTags = res.tags
    }
    const getSortListTag = async () => {
      const res = await reqSortListTag()
      data.sortListTag = res.sub
    }
    const itemCardClick = id => {
      router.push('/musiclistdetail/' + id)
    }
    const itemNavClick = v => {
      data.curTag = v
      getMusicList()
    }
    const itemBoxClick = item => {
      data.curTag = item
    }

    onMounted(async () => {
      getHighquality()
      await getHotTag()
      getMusicList()
    })
    return {
      ...toRefs(data),
      handleCurrentChange,
      itemBoxClick,
      itemCardClick,
      getSortListTag,
      itemNavClick
    }
  }
}
</script>

<style scoped>
.musicListRoot {
  width: 100%;
  height: 100%;
}
.navBar {
  padding: 10px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;

  font-size: 12px;
}
.left,
.right {
  display: flex;
  align-items: center;
}
.el-pagination {
  width: 100%;
  text-align: center;
}
</style>